package lib.core {
	
	import lib.config.ApplicationNotifications;
	import lib.config.RemoteServices;
	import lib.core.com.BaseApplication;
	import lib.core.commands.ModuleStartupCommand;
	import lib.core.commands.StartupCommand;
	
	import mx.rpc.remoting.RemoteObject;
	
	import org.puremvc.as3.interfaces.IFacade;
	import org.puremvc.as3.patterns.facade.Facade;
	
	public class ApplicationFacade extends Facade implements IFacade
	{		
		public static function getInstance():ApplicationFacade
		{
			if (instance == null) 
			{
				instance = new ApplicationFacade();
			}
			return instance as ApplicationFacade;
		}
		
		override protected function initializeController():void
		{
			// Here we initialize the controller and all commands that we need to operate
			// the system properly
			super.initializeController();
			// The StartupCommand handles model and view registration needed for operations
			// during startup of the application
			this.registerCommand(ApplicationNotifications.STARTUP, StartupCommand);
			// the ModuleStartupCommand handles UrlRule changes and loads the apropriate modules and their
			// actions
			this.registerCommand(ApplicationNotifications.MODULE_STARTUP, ModuleStartupCommand);
		}
		
		public function startup(app:BaseApplication):void
		{
			// Send a notification to the rest of the system that we're starting up
			this.sendNotification(ApplicationNotifications.STARTUP, app);
		}
		
		/**
		 * Static method to retrieve the system's remote service object. all what is needed 
		 * is to provide the name of the desired service
		 * @param service: The name of the desired service to be retrieved
		 * @return RemoteObject
		 * 
		 */
		public static function getRemoteObject(service:String):RemoteObject
		{
			var remote:RemoteObject = new RemoteObject(RemoteServices.REMOTE_SERVICE_ID);
			remote.source = service;
			return remote;
		}
	}
}